//#include <afxwin.h>
#include <stdlib.h>
#include "bmp.h"


int ReadHeader(FILE *infile, GFX_BITMAPINFOHEADER *i)
{
  GFX_BITMAPFILEHEADER head;

   if ((infile == NULL) || (i == NULL)) return 0;

  if (fread(&head.bfType, 2, 1, infile) != 1) return 0;
  if (fread(&head.bfSize, 4, 1, infile) != 1) return 0;
  if (fread(&head.bfReserved1, 2, 1, infile) != 1) return 0;
  if (fread(&head.bfReserved2, 2, 1, infile) != 1) return 0;
  if (fread(&head.bfOffBits, 4, 1, infile) != 1) return 0;


  if (fread(i, sizeof(GFX_BITMAPINFOHEADER), 1, infile) != 1) return 0;

  fseek(infile,head.bfOffBits-sizeof(GFX_BITMAPINFOHEADER)
		-HACKED_GFX_BITMAPFILEHEADER_SIZE, SEEK_CUR);
  
  return 1;
}

int WriteHeader(GFX_BITMAPINFOHEADER *i,FILE *outfile)
{
	GFX_BITMAPFILEHEADER bmfh;

	bmfh.bfType = *((unsigned short *) "BM");
	bmfh.bfOffBits = HACKED_GFX_BITMAPFILEHEADER_SIZE + sizeof(GFX_BITMAPINFOHEADER);
	if (i->biBitCount == 8)
		bmfh.bfOffBits += 256 * sizeof(GFX_RGBQUAD);
	bmfh.bfSize = bmfh.bfOffBits + i->biWidth * i->biHeight;

	bmfh.bfReserved1 = 0;
	bmfh.bfReserved2 = 0;
	if (fwrite(&bmfh.bfType, 2, 1, outfile) != 1) return 0;
	if (fwrite(&bmfh.bfSize, 4, 1, outfile) != 1) return 0;
	if (fwrite(&bmfh.bfReserved1, 2, 1, outfile) != 1) return 0;
	if (fwrite(&bmfh.bfReserved2, 2, 1, outfile) != 1) return 0;
	if (fwrite(&bmfh.bfOffBits, 4, 1, outfile) != 1) return 0;
	if (fwrite(i, sizeof(GFX_BITMAPINFOHEADER), 1, outfile) != 1) return 0;                                                                        

	if (i->biBitCount == 8)
	{
		if (fwrite(&i[1], 256 * sizeof(GFX_RGBQUAD), 1, outfile) != 1) return 0;
	}

	return 1;
}
/*
int WriteHeader(BITMAPINFOHEADER *i, FILE *outfile)
{
 int sizeofBITMAPFILEHEADER = 14;
 
 BITMAPFILEHEADER bmfh;
 
 bmfh.bfType = *((unsigned short *) "BM");
 bmfh.bfOffBits = sizeofBITMAPFILEHEADER + sizeof(BITMAPINFOHEADER);
 if (i->biBitCount == 8)
  bmfh.bfOffBits += 256 * sizeof(RGBQUAD);
 bmfh.bfSize = bmfh.bfOffBits + i->biWidth * i->biHeight;
 
 bmfh.bfReserved1 = 0;
 bmfh.bfReserved2 = 0;
 if (fwrite(&bmfh.bfType, 2, 1, outfile) != 1) return 0;
 if (fwrite(&bmfh.bfSize, 4, 1, outfile) != 1) return 0;
 if (fwrite(&bmfh.bfReserved1, 2, 1, outfile) != 1) return 0;
 if (fwrite(&bmfh.bfReserved2, 2, 1, outfile) != 1) return 0;
 if (fwrite(&bmfh.bfOffBits, 4, 1, outfile) != 1) return 0;
 if (fwrite(i, sizeof(BITMAPINFOHEADER), 1, outfile) != 1) return 0;                                                                        
 
 if (i->biBitCount == 8)
 {
  if (fwrite(&i[1], 256 * sizeof(RGBQUAD), 1, outfile) != 1) return 0;
 }
 
 return 1;
}
*/